package easystudy.admin;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import easystudy.dao.BoardDao;
import easystudy.dao.MemberDao;
import easystudy.dto.Board;
import easystudy.dto.Member;

@Controller
public class MemberDetailController {
	
	private MemberDao memberDao;
	
	private BoardDao boardDao;
	
	public void setMemberDao(MemberDao memberDao){
		this.memberDao = memberDao;
	}
	
	public void setBoardDao(BoardDao boardDao){
		this.boardDao = boardDao;
	}
	
	@RequestMapping(value="/member_detail.ad")
	public ModelAndView doMemberDetailProcess(@RequestParam("member_num")int member_num, @RequestParam("page")int page){
		
		int size=5;
		
		Member member = memberDao.getMemberDetail(member_num);
		
		int startRow = (page-1)*size+1;
		int endRow = startRow+size-1;
		
		List<Board> boardList = boardDao.getBoardListById(member_num, startRow, endRow);
		
		int boardCount = boardDao.getBoardCountById(member_num);
		
		int totalPageCount = (int)(boardCount/size);
		
		if(boardCount%size!=0)
			totalPageCount++;
		
		int start = ((int)(page/10))*10+1;
		int end = start+10-1;
		if(end>=totalPageCount)
			end=totalPageCount;
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("member_detail");
		mav.addObject("count",boardCount);
		mav.addObject("member", member);
		mav.addObject("maxpage",totalPageCount);
		mav.addObject("start",start);
		mav.addObject("current",page);
		mav.addObject("end",end);
		mav.addObject("member",member);
		mav.addObject("boardList",boardList);
		return mav;
	}

}
